package org.filecollector.msgcontroller.answers;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import lombok.NonNull;
import org.filecollector.msgcontroller.answers.converters.AnswerConverter;
import org.filecollector.msgcontroller.answers.responsecodes.ResponseCode;

public record AnswerMessage(
        @NonNull ResponseCode code,
        String seeOtherURI,
        String data
) {
    static Gson gsonParser = new GsonBuilder()
            .registerTypeAdapter(AnswerMessage.class, new AnswerConverter())
            .create();

    public static AnswerMessage deserialize(String json) {
        return gsonParser.fromJson(json, AnswerMessage.class);
    }

    public String serialize() {
        return gsonParser.toJson(this);
    }

    public JsonElement getAsElement() {
        return gsonParser.toJsonTree(this);
    }
}
